using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCSEngine.Utils.Entities.Process.Draw
{
    class DrawProcessManager : BaseGameProcessManager
    {
        private class DrawOrderComparer : IComparer<IDrawable>
        {
            private DrawOrderComparer()
            {
            }

            public static DrawOrderComparer Instance { get; private set; }

            static DrawOrderComparer()
            {
                Instance = new DrawOrderComparer();
            }

            #region IComparer<IDrawable> Members

            public int Compare(IDrawable x, IDrawable y)
            {
                if (x.DrawOrder > y.DrawOrder)
                    return 1;
                else if (x.DrawOrder == y.DrawOrder)
                    return 0;

                return -1;
            }

            #endregion
        }

        protected override bool IsValidProcess(object process)
        {
            return process is IDrawable;
        }

        public override void Run(Game game, GameTime gameTime)
        {
            List<IDrawable> DrawList = this.Processes.OfType<IDrawable>().ToList();
            DrawList.Sort(DrawOrderComparer.Instance);

            foreach (IDrawable DrawObject in DrawList)
            {
                if (DrawObject.Visible)
                {
                    DrawObject.Draw(gameTime);
                }
            }
        }

        public override bool IsDrawProcess
        {
            get { return true; }
        }

        public override Type ProcessType
        {
            get { return typeof(IDrawable); }
        }
    }
}
